﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Delay;
using System.ComponentModel;

namespace PhoneInfo.Page
{
    public partial class Most : PhoneApplicationPage
    {
        private ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        private List<Rankings> lsitrankings = new List<PhoneInfo.Page.Rankings>();
        private SearchResultView mainHotView = null;
        private SearchResultView mainNewView = null;
        bool l = true;
        public Most()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += new RoutedEventHandler(Most_Loaded);
            l = App.appSitting.Theme == 1;
            #region 热门排行的数组设置
            Rankings rk1 = new Rankings()
            {
                image = l ? "/Icon/product/r_01.jpg" : "/Icon/black/r_01.jpg",
                content = "热门手机排行榜"

            };
            lsitrankings.Add(rk1);
            Rankings rk2 = new Rankings()
            {
                image = l ? "/Icon/product/r_02.jpg" : "/Icon/black/r_02.jpg",
                content = "新机关注排行榜"

            };
            lsitrankings.Add(rk2);
            Rankings rk3 = new Rankings()
            {
                image = l ? "/Icon/product/r_03.jpg" : "/Icon/black/r_03.jpg",
                content = "手机降价排行榜"

            };
            lsitrankings.Add(rk3);
            Rankings rk4 = new Rankings()
            {
                image = l ? "/Icon/product/r_04.jpg" : "/Icon/black/r_04.jpg",
                content = "智能手机排行榜"
            };
            lsitrankings.Add(rk4);
            Rankings rk5 = new Rankings()
            {
                image = l ? "/Icon/product/r_05.jpg" : "/Icon/black/r_05.jpg",
                content = "android热门手机排行榜"
            };
            lsitrankings.Add(rk5);
            Rankings rk6 = new Rankings()
            {
                image = l ? "/Icon/product/r_06.jpg" : "/Icon/black/r_06.jpg",
                content = "WP平台排行榜"

            };
            lsitrankings.Add(rk6);
            Rankings rk7 = new Rankings()
            {
                image = l ? "/Icon/product/r_07.jpg" : "/Icon/black/r_07.jpg",
                content = "商务机排行榜"

            };
            lsitrankings.Add(rk7);
            Rankings rk8 = new Rankings()
            {
                image = l ? "/Icon/product/r_08.jpg" : "/Icon/black/r_08.jpg",
                content = "女性最爱机型排行榜"

            };
            lsitrankings.Add(rk8);
            Rankings rk9 = new Rankings()
            {
                image = l ? "/Icon/product/r_09.jpg" : "/Icon/black/r_09.jpg",
                content = "学生最常用机型排行榜"

            };
            lsitrankings.Add(rk9);
            Rankings rk10 = new Rankings()
            {
                image = l ? "/Icon/product/r_10.jpg" : "/Icon/black/r_10.jpg",
                content = "老人最爱机型排行榜"

            };
            lsitrankings.Add(rk10);
            Rankings rk11 = new Rankings()
            {
                image = l ? "/Icon/product/r_11.jpg" : "/Icon/black/r_11.jpg",
                content = "儿童机型排行榜"

            };
            lsitrankings.Add(rk11);
            #endregion
            rankings.DataContext = lsitrankings;
        }
        void Most_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = this._progress;
            this._progress.IsVisible = false;
            if (App.appSitting.Theme == 1)
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBar"];
            else
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBar1"];
        }
        private void Canvas_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.SerchPop.IsOpen = false;
            this.textresult.Focus();
            if (App.appSitting.Theme == 1)
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSearch"];
            else
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSearch1"];
        }
        private void ListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            phonePkItem pkItem = lb.SelectedItem as phonePkItem;
            if (pkItem == null) return;
            string objective = "/Page/TypeData.xaml";
            objective += "?pid=" + pkItem.Ponhe1_pid;
            objective += "&price=" + pkItem.Ponhe1_price;
            objective += "&maxprice=" + pkItem.Ponhe1_maxprice;
            objective += "&minprice=" + pkItem.Ponhe1_minprice;
            objective += "&picurl=" + pkItem.Ponhe1_picurl;
            objective += "&phonename=" + pkItem.Ponhe1_phonename;
            objective += "&ppname=" + pkItem.Ponhe1_ppname;
            NavigationService.Navigate(new Uri(objective, UriKind.Relative));
        }
        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender == null) return;
            Pivot pv = sender as Pivot;
            if (pv == null) return;
            switch (pv.SelectedIndex)
            {
                case 0:
                    if (App.appSitting.Theme != 1)
                    {
                        base.ApplicationBar = (ApplicationBar)base.Resources["AppBar1"];
                        break;
                    }
                    base.ApplicationBar = (ApplicationBar)base.Resources["AppBar"];
                    break;
                case 1:
                    if (this.mainHotView == null)
                    {
                        this.mainHotView = new SearchResultView();
                        this.mainHotView.ClassName = "mainNewView";
                        this.mainHotView.PropertyChanged += new PropertyChangedEventHandler(this.shrewd_PropertyChanged);
                        this.mainHotView.Result = 0;
                        this._progress.IsVisible = true;
                        this.mainHotView.CurrentPage = 1;
                        this.mainHotView.TotalPage = 0;
                        this.mainHotView.PageSize = 20;
                        this.mainHotView.AllowAdd = false;
                        this.mainHotView.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(this.mainHotView.GetPhoneInfoUrl(2)));
                        this.Hot.DataContext = this.mainHotView;
                    }
                    break;
                case 2:
                    if (this.mainNewView == null)
                    {
                        this.mainNewView = new SearchResultView();
                        this.mainNewView.ClassName = "mainNewView";
                        this.mainNewView.PropertyChanged += new PropertyChangedEventHandler(this.shrewd_PropertyChanged);
                        this.mainNewView.Result = 0;
                        this._progress.IsVisible = true;
                        this.mainNewView.CurrentPage = 1;
                        this.mainNewView.TotalPage = 0;
                        this.mainNewView.PageSize = 20;
                        this.mainNewView.AllowAdd = false;
                        this.mainNewView.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(this.mainNewView.GetPhoneInfoUrl(1)));
                        this.New.DataContext = this.mainNewView;
                    }
                    break;
            }
        }
        private void RankingTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox ls = sender as ListBox;
            if (ls == null)  return;
            string pagename = "/Page/HotPhone.xaml";
            pagename += "?classid=" + (ls.SelectedIndex + 1).ToString();
            NavigationService.Navigate(new Uri(pagename, UriKind.Relative));
        }
        private void search(object sender, EventArgs e)
        {
            if (App.appSitting.Theme == 1)
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSendSearch"];
            else
                base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSendSearch1"];
            this.SerchPop.IsOpen = true;
            textresult.Focus();
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (SerchPop.IsOpen)
            {
                SerchPop.IsOpen = false;
                e.Cancel = true;
                if (App.appSitting.Theme == 1)
                    base.ApplicationBar = (ApplicationBar)base.Resources["AppBar"];
                else
                    base.ApplicationBar = (ApplicationBar)base.Resources["AppBar1"];
            }
            base.OnBackKeyPress(e);
        }
        private void send(object sender, EventArgs e)
        {
            string content = this.textresult.Text;
            if (content.Length <= 0)
                MessageBox.Show("搜索的关键字不能为空");
            else
            {
                string pageName = "/Page/ProfectSearch.xaml";
                pageName = pageName + "?valuekey=" + content;
                base.NavigationService.Navigate(new Uri(pageName, UriKind.Relative));
                if (App.appSitting.Theme == 1)
                    base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSearch"];
                else
                    base.ApplicationBar = (ApplicationBar)base.Resources["AppBarSearch1"];
            }
        }
        private void shrewd_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender != null)
            {
                SearchResultView se = sender as SearchResultView;
                if ((se != null) && (e.PropertyName == "Result"))
                    this._progress.IsVisible = false;
            }
        }
        private void textresult_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            e.Handled = true;
        }
    }
    public class Rankings
    {
        public string image
        {
            get;
            set;
        }
        public string content
        {
            get;
            set;
        }
    };
}